﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using System.Web.Security;
using System.Data;

public partial class ExpertUsers_ReturnDevice : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        IDataReader Dr = null;
        var thisUser = Membership.GetUser(User.Identity.Name);
        if (!IsPostBack)
        {
            Dr = TblExpert.FetchByParameter(TblExpert.Columns.UserName, SubSonic.Comparison.Equals, Session["UserName"].ToString());
            if (Dr.Read())
            {

                Session["ExID"] = Dr["Id"].ToString();
            }
            Dr.Close();



        }

        //SdRequseDevice.SelectCommand = SdRequseDevice.SelectCommand + "ORDER BY TblDeviceExpert.[DateRequest] DESC ";
        GwRequestDevice.DataBind();


    }
    protected void GwRequestDevice_RowCommand(object sender, GridViewCommandEventArgs e)
    {



        if (e.CommandName == "ReturnRequest")
        {

            int IdReposDEtails = Convert.ToInt32(e.CommandArgument.ToString());
            SubSonic.Query Query = TblReposDetail.CreateQuery();
            Query.QueryType = SubSonic.QueryType.Update;
            Query.AddUpdateSetting(TblReposDetail.Columns.Available, "True");
            Query.WHERE(TblReposDetail.Columns.Id, IdReposDEtails);
            Query.Execute();

            TblDeviceExpert.Delete("ReposDetailsId", IdReposDEtails);
            lblMessage1.Text = "برگشت کالای درخواستی با موفقیت ثبت شد";
            GwRequestDevice.DataBind();
        }

    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (txtNumberRequest.Text == "" && txtRequstCust.Text=="" && DrDevice.SelectedValue.ToString()=="0")
        {

            SdRequseDevice.SelectCommand = SdRequseDevice.SelectCommand + "ORDER BY TblDeviceExpert.[DateRequest] DESC ";
            GwRequestDevice.DataBind();
        }
        else if (txtNumberRequest.Text != "" && txtRequstCust.Text == "" && DrDevice.SelectedValue.ToString() == "0")
        {
            SdRequseDevice.SelectCommand = SdRequseDevice.SelectCommand + "AND(TblDeviceExpert.[Id]=" + Convert.ToInt32(txtNumberRequest.Text) + ") ORDER BY TblDeviceExpert.[DateRequest] DESC";
            GwRequestDevice.DataBind();
        }
        else if (txtNumberRequest.Text == "" && txtRequstCust.Text != "" && DrDevice.SelectedValue.ToString()=="0")
        {
            SdRequseDevice.SelectCommand = SdRequseDevice.SelectCommand + "AND(TblDeviceExpert.[CustFolderId]=" + txtRequstCust.Text + ") ORDER BY TblDeviceExpert.[DateRequest] DESC";
            GwRequestDevice.DataBind();
        }
        else if (txtNumberRequest.Text == "" && txtRequstCust.Text == "" && DrDevice.SelectedValue.ToString() != "0")
        {
            SdRequseDevice.SelectCommand = SdRequseDevice.SelectCommand + "AND(TblDevice.[Id]=" + Convert.ToInt32(DrDevice.SelectedValue.ToString()) + ")";
            GwRequestDevice.DataBind();
        
        }
        else if (txtNumberRequest.Text != "" && txtRequstCust.Text != "" && DrDevice.SelectedValue.ToString() == "0")
        {
            SdRequseDevice.SelectCommand = SdRequseDevice.SelectCommand + "AND(TblDeviceExpert.[Id]=" + txtNumberRequest.Text + ")AND(TblDeviceExpert.[CustFolderId]=" + txtRequstCust.Text + ") ORDER BY TblDeviceExpert.[DateRequest] DESC";
            GwRequestDevice.DataBind();

        }
        else if (txtNumberRequest.Text != "" && txtRequstCust.Text == "" && DrDevice.SelectedValue.ToString() != "0")
        {
            SdRequseDevice.SelectCommand = SdRequseDevice.SelectCommand + "AND(TblDeviceExpert.[Id]=" + txtNumberRequest.Text + ")AND(TblDevice.[Id]=" + Convert.ToInt32(DrDevice.SelectedValue.ToString()) + ") ORDER BY TblDeviceExpert.[DateRequest] DESC";
            GwRequestDevice.DataBind();

        }
        else if (txtNumberRequest.Text == "" && txtRequstCust.Text != "" && DrDevice.SelectedValue.ToString() != "0")
        {
            SdRequseDevice.SelectCommand = SdRequseDevice.SelectCommand + "AND(TblDeviceExpert.[CustFolderId]=" + txtRequstCust.Text + ")AND(TblDevice.[Id]=" + Convert.ToInt32(DrDevice.SelectedValue.ToString()) + ") ORDER BY TblDeviceExpert.[DateRequest] DESC";
            GwRequestDevice.DataBind();

        }
        else if (txtNumberRequest.Text != "" && txtRequstCust.Text != "" && DrDevice.SelectedValue.ToString() != "0")
        {
            SdRequseDevice.SelectCommand = SdRequseDevice.SelectCommand + "AND(TblDeviceExpert.[Id]=" + txtNumberRequest.Text + ")AND(TblDeviceExpert.[CustFolderId]=" + txtRequstCust.Text + ")AND(TblDevice.[Id]=" + Convert.ToInt32(DrDevice.SelectedValue.ToString()) + ") ORDER BY TblDeviceExpert.[DateRequest] DESC";
            GwRequestDevice.DataBind();

        }
        txtRequstCust.Text = "";
        txtNumberRequest.Text = "";
        DrDevice.SelectedValue = "0";

       
       

    }
}